package com.talent.core.protocol.mqtt.codec.decoder;

import com.talent.core.protocol.mqtt.enums.TlMqttQos;
import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.payload.TlMqttPublishPayload;
import com.talent.core.protocol.mqtt.model.request.TlMqttPublishReq;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPublishVariableHead;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：TlConnectDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: 收到发送者的消息解码器
 */
public class TlMqttPublishDecoder {

    private static final  Logger logger = LoggerFactory.getLogger(TlMqttPublishDecoder.class);

    public TlMqttPublishReq build(ByteBuf buf, int type, int remainingLength){
        TlMqttFixedHead fixedHead = decodeFixedHeader( type, remainingLength);
        int qos = fixedHead.getQos();
        TlMqttQos tlMqttQos = TlMqttQos.valueOf(qos);
        TlMqttPublishVariableHead variableHead = decodeVariableHeader(buf,tlMqttQos);
        TlMqttPublishPayload payload = decodePayLoad(buf);
        return new TlMqttPublishReq(fixedHead,variableHead,payload);
    }

    TlMqttFixedHead decodeFixedHeader(int type,int remainingLength) {
        TlMqttFixedHead fixedHead =new TlMqttFixedHead();
        int messageType = type >> 4;
        int retain = (type) & 1;
        int qos = (type >> 1) & 3;
        int dup = (type >> 3) & 1;
        fixedHead.setMessageType(messageType);
        fixedHead.setType(type);
        fixedHead.setDup(dup);
        fixedHead.setQos(qos);
        fixedHead.setRetain(retain);
        fixedHead.setLength(remainingLength);
        logger.debug("解析【PUBLISH】的固定头:type=【{}】,message=【{}】,dup=【{}】,qos=【{}】,retain=【{}】，length=【{}】",type,messageType,dup,qos,retain,remainingLength);
        return fixedHead;
    }


    TlMqttPublishVariableHead decodeVariableHeader(ByteBuf buf, TlMqttQos qos) {
        TlMqttPublishVariableHead variableHead = new TlMqttPublishVariableHead();
        int topicLength = buf.readUnsignedShort();
        byte[] topic=new byte[topicLength];
        buf.readBytes(topic);
        String topicName = new String(topic);
        variableHead.setTopic(topicName);
        if(qos!=TlMqttQos.QOS0){
            int messageId = buf.readUnsignedShort();
            variableHead.setMessageId(messageId);
        }
        logger.debug("解析【PUBLISH】的可变头:topic是【{}】,消息ID是【{}=",variableHead.getTopic(),variableHead.getMessageId());
        return variableHead;
    }


    TlMqttPublishPayload decodePayLoad(ByteBuf buf) {
        int contentLength = buf.readableBytes();
        byte[] contentByte =new byte[contentLength];
        buf.readBytes(contentByte);
        String content = new String(contentByte);
        TlMqttPublishPayload publishPayload = new TlMqttPublishPayload();
        publishPayload.setContent(content);
        logger.debug("解析【PUBLISH】的payload【{}】",content);
        return publishPayload;
    }
}
